﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TotalCrytography.Protector;
using System.Xml;
using System.Security.Cryptography;
using System.Collections;

namespace TotalCrytography.Data
{
    class UserBUS:UserDAO
    {
        public static bool Import(string path)
        {
            // import account information from xml file
            XmlDocument info = new XmlDocument();
            info.Load(path);
            XmlElement root = info.DocumentElement;
            XmlElement emailNode = (XmlElement)root.ChildNodes[0];
            XmlElement hashPassNode = (XmlElement)root.ChildNodes[1];
            XmlElement nameNode = (XmlElement)root.ChildNodes[2];
            XmlElement birthdayNode = (XmlElement)root.ChildNodes[3];
            XmlElement phoneNode = (XmlElement)root.ChildNodes[4];
            XmlElement addressNode = (XmlElement)root.ChildNodes[5];
            XmlElement saltNode = (XmlElement)root.ChildNodes[6];
            XmlElement publicKeyNode = (XmlElement)root.ChildNodes[7];
            XmlElement privateKeyNode = (XmlElement)root.ChildNodes[8];

            UserDTO userDto = new UserDTO();
            userDto.Email = emailNode.GetAttribute("value");
            userDto.HashPass = hashPassNode.GetAttribute("value");
            userDto.Name = nameNode.GetAttribute("value");
            userDto.Birthday = birthdayNode.GetAttribute("value");
            userDto.Phone = phoneNode.GetAttribute("value");
            userDto.Address = addressNode.GetAttribute("value");
            userDto.Salt = saltNode.GetAttribute("value");
            //userDto.PublicKey = publicKeyNode.InnerXml;
            userDto.PublicKey = publicKeyNode.GetAttribute("value");
            userDto.PrivateKey = privateKeyNode.GetAttribute("value");

            UserDAO userDao = new UserDAO();
            if (!userDao.IsExisted(userDto.Email))
            {
                userDao.Insert(userDto);
                return true;
            }
            return false;
        }

        public static bool AccountIsExisted(string strEmail)
        {
            UserDAO userDao = new UserDAO();
            return userDao.IsExisted(strEmail);
        }

        public static bool Export(UserDTO userDto, string path, string passphrase)
        {
            // export account info to xml file
            XmlDocument doc = new XmlDocument();
            //string public key = "<PublicKey>" + userDto.PublicKey + "</PublicKey>";
            //doc.LoadXml(publickey);
            //XmlElement publicKeyNode = (XmlElement)doc.FirstChild;
            string strCreate = "<UserInfo></UserInfo>";
            doc.LoadXml(strCreate);
            doc.RemoveAll();
            XmlElement root = doc.CreateElement("UserInfo");
            doc.AppendChild(root);
            XmlElement emailNode = doc.CreateElement("Email");
            emailNode.SetAttribute("value", userDto.Email);
            XmlElement hashPassNode = doc.CreateElement("HashPass");
            hashPassNode.SetAttribute("value", userDto.HashPass);
            XmlElement nameNode = doc.CreateElement("Name");
            nameNode.SetAttribute("value", userDto.Name);
            XmlElement birthdayNode = doc.CreateElement("Birthday");
            birthdayNode.SetAttribute("value", userDto.Birthday);
            XmlElement phoneNode = doc.CreateElement("Phone");
            phoneNode.SetAttribute("value", userDto.Phone);
            XmlElement addressNode = doc.CreateElement("Address");
            addressNode.SetAttribute("value", userDto.Address);
            XmlElement saltNode = doc.CreateElement("Salt");
            saltNode.SetAttribute("value", userDto.Salt);
            XmlElement privateKeyNode = doc.CreateElement("PrivateKey");
            //string privateKey = EncryptPrivateKey(userDto.PrivateKey, passphrase); // userDto-encrypt private key before export
            privateKeyNode.SetAttribute("value", userDto.PrivateKey);
            XmlElement publicKeyNode = doc.CreateElement("PublicKey");
            publicKeyNode.SetAttribute("value", userDto.PublicKey);
            root.AppendChild(emailNode);
            root.AppendChild(hashPassNode);
            root.AppendChild(nameNode);
            root.AppendChild(birthdayNode);
            root.AppendChild(phoneNode);
            root.AppendChild(addressNode);
            root.AppendChild(saltNode);
            root.AppendChild(publicKeyNode);
            root.AppendChild(privateKeyNode);
            doc.Save(path);
            return true;
        }

        public static ArrayList GetListUsers()
        {
            UserDAO userDao = new UserDAO();
            return userDao.SelectAll();
        }

        public static UserDTO GetUser(string strEmail)
        {
            UserDAO userDao = new UserDAO();
            return userDao.GetUser(strEmail);
        }

        public static bool Insert(UserDTO userDto)
        {
            UserDAO userDao = new UserDAO();
            if (!userDao.IsExisted(userDto.Email))
            {
                userDao.Insert(userDto);
                return true;
            }
            return false;
        }

        public static void Update(UserDTO userDto)
        {
            UserDAO userDao = new UserDAO();
            userDao.Update(userDto);
        }
 
    }
 
}
